package com.neulogistics.contrib.usercenter.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.Role;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserRole;
import com.neulogistics.contrib.usercenter.infrastructure.persistence.orm.po.RolePO;
import org.springframework.beans.BeanUtils;

public class RoleEntityPOConverter {
    public static RolePO entityToPO(Role role) {
        if (role == null)
            return null;

        RolePO rolePO = new RolePO();
        // 复制普通属性
        BeanUtils.copyProperties(role, rolePO);
        // 枚举值转化
        rolePO.setRoleLevel(role.getRoleLevel().id);
        return rolePO;
    }

    public static Role POToEntity(RolePO rolePO) {
        if (rolePO == null)
            return null;

        Role role = new Role();
        // 复制普通属性
        BeanUtils.copyProperties(rolePO, role);

        // 枚举值转化
        role.setRoleLevel(UserRole.fromValue(
                rolePO.getRoleLevel(), UserRole.class));

        return role;
    }
}
